#include <QikEditCategoriesDlg.h>

#include "PinQAppUi.h"
#include "PinQDocument.h"
#include "PinQListView.h"
#include "PinQTaskView.h"
#include "PinQPacketsView.h"
#include "PinQExternalInterface.h"
#include "PinQ.hrh"
#include <PinQ.rsg>

CPinQAppUi::CPinQAppUi()
	{
	}
CPinQAppUi::~CPinQAppUi()
	{
	delete iCategoryModelInstance;
	}
void CPinQAppUi::ConstructL()
	{
	BaseConstructL();

	iPinQListView = CPinQListView::NewLC(*this, KNullViewId);
	AddViewL(*iPinQListView);
	CleanupStack::Pop(iPinQListView);
	
	iPinQTaskView = CPinQTaskView::NewLC(*this,TVwsViewId(KUidPinQApp, KUidPinQListView));
	AddViewL(*iPinQTaskView);
	CleanupStack::Pop(iPinQTaskView);
	
	iPinQPacketsView = CPinQPacketsView::NewLC(*this,TVwsViewId(KUidPinQApp, KUidPinQPacketsView));
	AddViewL(*iPinQPacketsView);
	CleanupStack::Pop(iPinQPacketsView);

	iCategoryModelInstance = QikCategoryUtils::ConstructCategoriesLC(R_PINQ_DEFAULT_CATEGORIES);
	CleanupStack::Pop();
	
	iCurrentCategoryHandle = EPinQCategoryAll;
	}

CQikCategoryModel* CPinQAppUi::CategoryModelInstance()
	{
	return iCategoryModelInstance;
	}

TInt CPinQAppUi::CurrentCategoryHandle() const 
	{
	return iCurrentCategoryHandle;
	}

void CPinQAppUi::SetCurrentCategoryHandle(TInt aCategoryHandle)
	{
	if (iCategoryModelInstance->CategoryIndex(aCategoryHandle) != KErrNotFound)
		{
		iCurrentCategoryHandle = aCategoryHandle;
		}
	else 
		{
		iCurrentCategoryHandle = iCategoryModelInstance->HandleOfUnfiledCategory();
		}
	}

void CPinQAppUi::HandleCategoryChangeL(TInt aCategoryHandle)
	{
	if (aCategoryHandle != CurrentCategoryHandle())
		{
		SetCurrentCategoryHandle(aCategoryHandle);
		CPinQListView* myDirectoryListView = CurrentView<CPinQListView>();
		myDirectoryListView->RefreshListBoxL();
		}
	}

void CPinQAppUi::EditCategoryL()
	{
	CQikEditCategoriesDialog::RunDlgLD(iCategoryModelInstance, this);
	}
	
TBool CPinQAppUi::OkToAddCategory() const
	{
	return ETrue;
	}

TBool CPinQAppUi::DoAddCategoryL(TInt& aHandle)
	{
	TQikCategoryName categoryName = iCategoryModelInstance->CategoryNameByHandle(aHandle);
	PinQModel().AddCategory(categoryName,aHandle);
	CategoryChangedL();
	return ETrue;
	}

TBool CPinQAppUi::OkToRenameCategory(TInt /*aHandle*/, const TDesC& /*aNewName*/) const
	{
	return ETrue;
	}

TBool CPinQAppUi::DoRenameCategoryL(TInt aHandle, const TDesC& aNewName)
	{
	PinQModel().RenameCategory(aHandle, aNewName);
	CategoryChangedL();
	return ETrue;
	}

TBool CPinQAppUi::OkToMergeCategories(TInt /*aSourceHandle*/, TInt /*aTargetHandle*/) const
	{
	return EFalse;
	}

TBool CPinQAppUi::DoMergeCategoriesL(TInt /*aSourceHandle*/, TInt /*aTargetHandle*/)
	{
	return EFalse;
	}

TBool CPinQAppUi::OkToDeleteCategory(TInt /*aHandle*/) const
	{
	return ETrue;
	}

TBool CPinQAppUi::DoDeleteCategoryL(TInt aHandle)
	{
	PinQModel().RemoveCategory(aHandle);
	CategoryChangedL();
	
	if(aHandle == iCurrentCategoryHandle)
		{
		iCurrentCategoryHandle = EPinQCategoryAll;
		CPinQListView* myDirectoryListView = CurrentView<CPinQListView>();
		myDirectoryListView->RefreshListBoxL();
		}
	return ETrue;
	}

TBool CPinQAppUi::IsCategoryEmpty(TInt /*aHandle*/) const
	{
	return ETrue;
	}

void CPinQAppUi::SetChoiceListDirty()
	{
	TVwsViewId viewId = TVwsViewId(KUidPinQApp, KUidPinQTaskView);
	CPinQTaskView* detailView = (CPinQTaskView*)QikView(viewId);
	detailView->SetChoiceListDirty(ETrue);
	}

void CPinQAppUi::CategoryChangedL()
	{
	CPinQDocument* document = static_cast<CPinQDocument*>(iDocument);
	document->SaveL();

	SetChoiceListDirty();
	}

void CPinQAppUi::TaskStateChangedL(const CPinQTask& aTask,CPinQTask::TState aNewState)
	{
	if (iPinQListView->IsActivated())
		iPinQListView->TaskStateChangedL(aTask,aNewState);
	else
		if (iPinQPacketsView->IsActivated() && aNewState==CPinQTask::EReceived)
			{
		    ASSERT(aTask.LastPacket());
			iPinQPacketsView->PacketRecievedL(*(aTask.LastPacket()));
			}
	};

